﻿using System.IO;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Script.Serialization;

namespace WCFServices
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "R2Services" in code, svc and config file together.
    public class R2Services : IR2Services
    {
        public Stream HelloWorld(string name)
        {
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");

            return WriteJson(new
            {
                Id = 1,
                Name = name,
                Dreams = "None"
            });
        }

        private Stream WriteJson(object value)
        {
            var javaScriptSerializer = new JavaScriptSerializer();
            var json = Encoding.UTF8.GetBytes(javaScriptSerializer.Serialize(value));
            var memoryStream = new MemoryStream(json);
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            return memoryStream;
        }
    }
}
